#!/usr/bin/python

#Copyright 2012 Elias Farhan
#
#This file is part of Squirrel.
#
#Squirrel is free software: you can redistribute it and/or modify it under the terms of the GNU General 
#Public License as published by the Free Software Foundation, either version 3 of the License, or (at your 
#option) any later version.
#
#Squirrel is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
#without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
#See the GNU General Public License for more details.

from gameobject import *
from box import *

class Game():
	def __init__(self, screen_size):
		self.screen_size = screen_size
		self.imglist = {}
		self.screenpos = (0, 0)
	def setSize(self, screen_size):
		self.screen_size = screen_size
	def initgame(self):
		self.todo1()
	def addplayer(self, dirname):
		size = (225, 150)
		backsize = self.background.size
		self.player = Character((backsize[0]/2, backsize[1]/2), size, dirname)
		self.importImg(dirname, 4, 4, size)
		self.player.setImage(self.imglist[dirname])
		self.screenpos = (self.player.pos[0]-self.screen_size[0]/2, self.player.pos[1]-self.screen_size[1]/2)
		self.box = Box((backsize[0]/2, backsize[1]/2), (backsize[0]/2+size[0], backsize[1]/2+size[1]))

	def manageKey(self, k, isUp):
		if (k == 'w'):
			self.player.setCurrent(3, isUp)
		elif (k == 'd'):
			self.player.setCurrent(1, isUp)
		elif (k == 'a'):
			self.player.setCurrent(2, isUp)
		elif (k == 's'):
			self.player.setCurrent(4, isUp)
		elif (k == 'e'):
			self.player.setState(1, isUp)
	def updateScene(self):
		#print "NEW"
		self.screenpos = (self.player.pos[0]-self.screen_size[0]/2, self.player.pos[1]-self.screen_size[1]/2)
		self.box.setPosition(self.player.pos, (self.player.pos[0]+self.player.size[0], self.player.pos[1]+self.player.size[1]))
		boxes = []
		for box in self.background.getBoxes():
			if self.box.BoxIsIn(box):
				#print "player big box", self.box.origin, self.box.end, "near", box.origin, box.end
				boxes.append(box)
		self.player.setNearBox(boxes)
		self.player.update()
	def drawScene(self, screen):
		#todo1
		screen.blit(self.background.image, (self.screen_size[0]/2-self.player.pos[0]-self.player.size[0]/2, self.screen_size[1]/2-self.player.pos[1]-self.player.size[1]/2))
		screen.blit(self.player.image, 
		(self.screen_size[0]/2-self.player.size[0]/2, self.screen_size[1]/2-self.player.size[1]/2))
		#/todo1
	def importImg(self, filename, dimension, frames, size):
		img = []
		try:
			img = self.imglist[filename]
		except KeyError:
			#import the img from HD
			for k in xrange(1, 3):
				for i in xrange(1, dimension+1):
					for j in xrange(1, frames+1):
						img.append(load_image(filename+str(k)+str(i)+str(j)+".png", size))
			self.imglist[filename] = img
	def todo1(self):
		#used to create the world, will be replaced by an xml version
		if (self.screen_size == (0, 0)):
			raise Exception("Error screen not initialized")
		original_size = (9000, 2550)
		ratio = 4
		self.background = GameObject((0, 0), (original_size[0]/ratio, original_size[1]/ratio), "prout")
		self.background.setImage("data/sprites/bateau/coque.png")
		self.background.addBoxes(Box((2332/ratio, 1881/ratio), (7150/ratio, 2200/ratio)))
		self.background.addBoxes(Box((706, 180), (1658, 230)))
		self.addplayer("data/sprites/soldat/arclesien")
	def testmouse(self, pos):
		print "mouse pos", (pos[0]+self.screenpos[0], pos[1]+self.screenpos[1])
		
		
